import "../css/write.scss";
// import {markdown} from 'markdown';
import Render from "./render";
const md = require('markdown-it')();
const out = document.getElementById("write_out");
let dataHTML = "";
let originData = "";
let id = null;
let Model = {
    id:id,
    uid:1,
};
//监听上传文件的input框
$("#file_input").change(function (e) {
    let formdata = new FormData();
    let ee = e;
    formdata.append('file', e.target.files[0]);
    $.ajax({
        url: 'https://file.yaop.ink/upload',
        method: 'POST',
        async: false,
        data: formdata,
        processData: false,
        contentType: false,
        success: function (e) {
            let img = document.createElement("img");
            img.src = e.url;
            dataHTML = dataHTML + img.outerHTML;
            out.innerHTML = dataHTML;
            insertText(document.getElementById("write_in"),`![${ee.target.files[0].name}](${img.src})\n`)
            // document.getElementById("write_in").value = document.getElementById("write_in").value + `![${ee.target.files[0].name}](${img.src})\n`;
        }
    });
});
$("#uploadImg").click(function (e) {
    $("#file_input").click();
})
document.getElementById("write_in").onkeydown = function(e){
    if(e.keyCode===9){
        insertText(e.target,`    `)
        e.returnValue =false
    }else{
        originData = e.target.value;
        dataHTML = md.render(e.target.value);
        out.innerHTML = dataHTML
    }
}
// document.getElementById("write_in").oninput = function (e) {
//     originData = e.target.value;
//     dataHTML = md.render(e.target.value);
//     out.innerHTML = dataHTML
//
// };
function insertText(obj,str) {
    if (document.selection) {
        var sel = document.selection.createRange();
        sel.text = str;
    } else if (typeof obj.selectionStart === 'number' && typeof obj.selectionEnd === 'number') {
        var startPos = obj.selectionStart,
            endPos = obj.selectionEnd,
            cursorPos = startPos,
            tmpStr = obj.value;
        obj.value = tmpStr.substring(0, startPos) + str + tmpStr.substring(endPos, tmpStr.length);
        cursorPos += str.length;
        obj.selectionStart = obj.selectionEnd = cursorPos;
    } else {
        obj.value += str;
    }
}
let category = new Render(".category-main-list");

function init() {
    id = /id=[0-9]*&{0}/.exec(window.location.href);
    if (id != null) {
        id = id[0].split("=")[1];
        $.ajax({
            url: "/manager/get/article",
            data: {id},
            success: function (e) {
                Model = e;
                $('#art_title').val(e.title);
                $('#art_category').val(e.category);
                $.ajax({
                    url: "/api/category/get",
                    success: function (ee) {
                        category.for(ee, {cg: e.category});
                        document.getElementsByClassName("category-main-list")[0].childNodes.forEach(function (ex) {
                            if (parseInt(ex.value) === e.category) {
                                $(ex).attr("selected", "selected");
                            }
                        })
                    }
                });
                document.getElementById("art_is_del").checked = e.is_delete === 1 ? true : false;
                let ht = e.origin_body;
                originData = ht;
                document.getElementById("write_in").value = ht;
                dataHTML = md.render(ht);
                out.innerHTML = dataHTML;
            }
        });
    } else {
        $.ajax({
            url: "/api/category/get",
            success: function (ee) {
                category.for(ee);
            }
        });
    }
}

init();
$('#save').click(function () {
    let data = new FormData();
    if (id !== null) {
        data.append("id", Model.id);
    }
    data.append("uid", Model.uid);
    data.append("author", "tong");
    data.append("title", $('#art_title').val());
    data.append("origin_body", originData);
    data.append("body", dataHTML);
    data.append("category", $('.category-main-list').val());
    data.append("is_delete", document.getElementById("art_is_del").checked ? 1 : 0);
    $.ajax({
        method: 'POST',
        url: "/manager/post_article",
        async: false,
        data: data,
        processData: false,
        contentType: false,
        success: function (e) {
            alert(e);
            window.reload();
        }
    });
});